package guiClasses;
/**
 * TCSS 360 - Project
 */
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Observable;
import java.util.Observer;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;

import objectClasses.User;

/**
 *  A generic panel class that extends Observable.
 *  
 *  @author Abraham Santini
 */
abstract class GenericPanel extends Observable{
	/**
	 * The observer object that this class will notify.
	 */
	protected Observer my_o_ver;
	/**
	 * A JPanel to hold all of the objects that the various child classes will need.
	 */
	protected JPanel my_panel;
	
	/**
	 * Public constructor for the GenericPanel class.
	 * @param the_o_er The Observer object that this calls will notify.
	 */
	public GenericPanel(Observer the_o_er) {
		my_panel = new JPanel();
		my_o_ver = the_o_er;
		addObserver(my_o_ver);
		my_panel.setLayout(new GridBagLayout());
		JPanel p = new JPanel(new GridBagLayout());
		if(GuiMain.getUser() != null)
		{
			p.add(new JLabel("Welcome " + GuiMain.getUser().getUserName()), GBC.GBCGen(0, 0, 0, 0));
		
			JButton logoutButton = new JButton("Logout");
			logoutButton.setForeground(Color.BLUE);
			logoutButton.setBorderPainted(false);
			logoutButton.setOpaque(false);
			logoutButton.setBackground(new Color(139, 101, 219));
			logoutButton.setCursor(new Cursor(Cursor.HAND_CURSOR));
			logoutButton.addActionListener(new ActionListener(){

				@Override
				public void actionPerformed(ActionEvent arg0) {
					GuiMain.setUser(null);
					setChanged();
					notifyObservers(new LoginPanel(my_o_ver).getPanel());	
				}
			});
			p.add(logoutButton, GBC.GBCGen(0,0,1,0)); // 5 is just to push it to the far right
		}
		else
		{
			p.add(new JLabel("You are logged out"), GBC.GBCGen(0,0,0,0));
			JButton loginButton = new JButton("Login");
			loginButton.setForeground(Color.BLUE);
			loginButton.setBorderPainted(false);
			loginButton.setOpaque(false);
			loginButton.setBackground(new Color(139, 101, 219));
			loginButton.setCursor(new Cursor(Cursor.HAND_CURSOR));
			loginButton.addActionListener(new ActionListener(){

				@Override
				public void actionPerformed(ActionEvent arg0) 
				{
					setChanged();
					notifyObservers(new LoginPanel(my_o_ver).getPanel());	
				}
			});
			p.add(loginButton, GBC.GBCGen(0,0,1,0));
		}
		GridBagConstraints g = GBC.GBCGen(0,0,0,0);
		g.anchor = GridBagConstraints.LINE_START;
		g.gridwidth = 2;
		my_panel.add(p, g);
		my_panel.setPreferredSize(new Dimension(630, 290));
	}
	
	/**
	 * Returns the class' JPanel
	 * @return my_panel The panel that holds the user specific objects.
	 */
	public JPanel getPanel() {
		return my_panel;
	}
	
}
